package com.broadtext.rms.system.service.impl;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.system.model.TtPackWeeklyReport;
import com.broadtext.rms.system.service.ITtPackWeeklyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ttPackWeeklyReportService")
public class TtPackWeeklyReportServiceImpl extends EntityServiceImpl<TtPackWeeklyReport> implements ITtPackWeeklyReportService {

    @Autowired
    @Qualifier("ttPackWeeklyReportDao")
    public void setEntityDao(EntityDao<TtPackWeeklyReport> entityDao) {
        super.entityDao = entityDao;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Object>> findWeeklyReport(Map<String, Object> params) throws DaoException {
        List<Map<String, Object>> list = this.entityDao.findByProperty("com.broadtext.rms.system.model.TtPackWeeklyReport.selectWeeklyReport", params);
        for (Map<String, Object> t : list) {
            if (t != null && StringUtils.isNotBlank(t.get("packCode") + "") && StringUtils.isNotBlank(t.get("colClientId") + "")) {
                params.put("packCode", t.get("packCode"));
                params.put("colClientId", t.get("colClientId"));
                List<Map<String, Object>> alertData = this.entityDao.findByProperty("com.broadtext.rms.system.model.TtPackWeeklyReport.selectAlertType", params);
                if (alertData != null && alertData.size() > 0) {
                    String alert = "";
                    for (int i = 0; i < alertData.size(); i++) {
                        alert = alert + alertData.get(i).get("name") + ":" + alertData.get(i).get("value");
                        if (i < alertData.size() - 1) {
                            alert = alert + ",";
                        }
                    }
                    System.out.println(alertData.size() + "  " + alert);
                    t.put("alertData", alert);
                }

            }
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Object>> findAlertDetail(String packCode, String startDate, String endDate) throws DaoException {
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(packCode)) {
            params.put("packCode",packCode);
        }
        if(StringUtils.isNotBlank(startDate)) {
            params.put("startDate",startDate);
        }
        if(StringUtils.isNotBlank(endDate)) {
            params.put("endDate",endDate);
        }
        return (List<Map<String, Object>>) this.entityDao.findByProperty("com.broadtext.rms.system.model.TtPackWeeklyReport.selectAlertType", params);
    }
}
